package com.community.service;

import com.alibaba.fastjson.JSONObject;
import com.community.entity.ProblemUser;
import com.community.entity.Student;
import com.community.service.pojo.CharacterResult;
import com.community.service.pojo.InterestResult;
import com.feng.fengDUtil.BaseService;
import com.community.util.Search;
import com.feng.fengDUtil.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class QuizService extends BaseService{

    @Autowired
    private StudentService studentService;

    public Page query(Page page, Search search) {
        ArrayList params = new ArrayList();
        String sql = "select * from a_problem u  where 1=1 ";
        if(search.getGtype()!=null){
            sql+=" and u.gtype = ? ";
            params.add(search.getGtype());
        }
        sql+=" order by gtype asc, u.num asc";
        findPageList(Map.class,sql,page,params.toArray());
        return page;
    }

    public List<ProblemUser> allProblems(Student student, Integer gtype){
        String sql ="select p.num,p.title,p.mtype,p.gtype,p.select_problem,p.select_result,s.id,s.result from a_problem p " +
                " left join a_problem_user s on s.uid = ? and p.num = s.num and p.gtype = s.gtype where p.gtype = ? ";
        return sql(sql).list(ProblemUser.class,student.getId(),gtype);
    }

    public boolean analysis(List<ProblemUser> list, JSONObject ret){
        boolean finish;
        String gtype = list.get(0).getGtype();
        ret.put("gtype",gtype);

        if("1".equals(gtype)){
            finish = analysisInterest(list,ret);
        }else{
            finish = analysisCharacter(list,ret);
        }
        updateTestResult(ret,gtype);
        return finish;
    }

    private void updateTestResult(JSONObject ret,String gtype){
        Student student = studentService.curStudent();
        if(student!=null){
            if("1".equals(gtype)){
                student.setTestInterest(ret.getString("resultText"));
            }else{
                student.setTestCharacter(ret.getString("resultText"));
            }
            studentService.update(student);
        }
    }

    private boolean analysisInterest(List<ProblemUser> list, JSONObject ret) {
        //累计分数
        LinkedHashMap<String, Integer> dataMap=new LinkedHashMap<>();
        String[] categories = {"R","I","A","S","E","C"};
        list.forEach(n->{
            if("1".equals(n.getResult())){

                String category = null;
                for(String str:categories){
                    if(n.getMtype().contains(str)){
                        category = str;
                        break;
                    }
                }
                if(category!=null){
                    int cnt =   dataMap.getOrDefault(category,0);
                    dataMap.put(category,++cnt);
                }
            }
        });
        List<Map.Entry<String,Integer>> array = new ArrayList<>();
        array.addAll(dataMap.entrySet());
        array.sort((u1, u2) -> u2.getValue().compareTo(u1.getValue()));

        List<String> characterList=new ArrayList<String>();
        if(array.size()>=3){
            String groupTag = array.get(0).getKey()+array.get(1).getKey()+array.get(2).getKey();
            String desc = InterestResult.map.get(groupTag);
            if(desc!=null){
                characterList.add(groupTag+"型<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+desc);
            }
            ret.put("resultText", groupTag);
            for(int i =0;i<3;i++){
                characterList.add(InterestResult.getTestResultStr(array.get(i).getKey()));
            }
        }
        ret.put("dataMap",dataMap);
        ret.put("characterList", characterList);
        return true;
    }

    public boolean analysisCharacter(List<ProblemUser> list, JSONObject ret){
        //累计分数
        HashMap<String, Integer> dataMap=new HashMap<>();
        //List<String> categories = Arrays.asList("E","I","S","N","T","F","J","P");
        list.forEach(n->{
            String key = n.getResult().trim();
            if(key!=null){
                int cnt =   dataMap.getOrDefault(key,0);
                dataMap.put(key,++cnt);
            }
        });
        String groupTag = "";
        groupTag+= dataMap.getOrDefault("E",0)>=dataMap.getOrDefault("I",0)?"E":"I";
        groupTag+= dataMap.getOrDefault("S",0)>=dataMap.getOrDefault("N",0)?"S":"N";
        groupTag+= dataMap.getOrDefault("T",0)>=dataMap.getOrDefault("F",0)?"T":"F";
        groupTag+= dataMap.getOrDefault("J",0)>=dataMap.getOrDefault("P",0)?"J":"P";

        List<String[]> characterList = new ArrayList<String[]>();

        ret.put("resultText", groupTag);
        List<String> descList = CharacterResult.map.get(groupTag);
        // List<String> descList = CharacterResult.map.get("ENFJ");
        if(descList!=null && descList.size()>0){
            ret.put("introduce",descList.get(0));
            ret.put("feature", descList.get(1));
            ret.put("careerIntention", descList.get(2));
            for (Integer i = 3; i < descList.size(); i++) {
                characterList.add(descList.get(i).split("\\|"));
            }
        }
        ret.put("dataMap",dataMap);
        ret.put("characterList",characterList);
        return true;
    }

}
